package com.lyw.home

import android.content.Context
import android.graphics.Color
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.gyf.immersionbar.ImmersionBar
import com.lu.library.base.BaseFragment
import com.lyw.home.bean.KnowledgeTypeInfo
import com.lyw.home.databinding.FragmentHomeBinding
import com.lyw.home.databinding.ItemClassBinding
import com.lyw.home.databinding.ItemKnowledgeTypeBinding
import dagger.hilt.android.AndroidEntryPoint
import me.shouheng.utils.ktx.onDebouncedClick


@Route(path = HomeRouterPaths.HOME_MAIN)
@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding>() {

    private lateinit var homeVm :HomeVm
    private lateinit var konwledgeTypeInfoAdapter: BaseQuickAdapter<KnowledgeTypeInfo,DataBindingHolder<ItemClassBinding>>

    override fun initListener() {
        super.initListener()
        binding.clSearch.onDebouncedClick {
            // 搜索按钮点击事件
        }
//        binding.btnJvm.onDebouncedClick {
//            HomeRouterPaths.HOME_QUESTION_SELECTION.navigation("KnowledgeType" , KnowledgeType.Jvm.ordinal)
//        }
//        binding.btnJava.onDebouncedClick {
//            HomeRouterPaths.HOME_QUESTION_SELECTION.navigation("KnowledgeType" , KnowledgeType.JavaLanguage.ordinal)
//        }
//        binding.btnJavaThread.onDebouncedClick {
//            HomeRouterPaths.HOME_QUESTION_SELECTION.navigation("KnowledgeType" , KnowledgeType.JavaThread.ordinal)
//        }
        binding.btnMode.onDebouncedClick {
            homeVm.toggleMode()
        }
    }

    override fun initViews() {
        super.initViews()
        ImmersionBar.with(this)
            .statusBarColor(("#2196F3"))
            .init()
    }
    override fun initData() {
        homeVm = ViewModelProvider(this).get(HomeVm::class.java)
        binding.setVariable(BR.vm, homeVm)
        homeVm.initDb(requireContext())
        homeVm.questionModeDes.observe(this){
            binding.btnMode.text = it
        }
        setRecyclerView()
    }
    private fun setRecyclerView(){
        var adapter = object:BaseQuickAdapter<ContentItem, DataBindingHolder<ItemKnowledgeTypeBinding>>(mutableListOf<ContentItem>()){
            override fun onBindViewHolder(holder: DataBindingHolder<ItemKnowledgeTypeBinding>, position: Int, item: ContentItem?) {
                holder.binding.btnType.text = (item?.name)
                holder.binding.btnType.onDebouncedClick {
                    homeVm.itemClick(position,item!!);
                }
            }

            override fun onCreateViewHolder(
                context: Context,
                parent: ViewGroup,
                viewType: Int
            ): DataBindingHolder<ItemKnowledgeTypeBinding> {
                return DataBindingHolder(R.layout.item_knowledge_type,parent)
            }

        }
        binding.recyclerView.layoutManager  = LinearLayoutManager(context)
        binding.recyclerView.adapter = adapter
        homeVm.learnTypes.observe(viewLifecycleOwner){
            adapter.items = it
            adapter.notifyDataSetChanged()
        }

        konwledgeTypeInfoAdapter = object :BaseQuickAdapter<KnowledgeTypeInfo,DataBindingHolder<ItemClassBinding>>(mutableListOf<KnowledgeTypeInfo>()){
            override fun onBindViewHolder(holder: DataBindingHolder<ItemClassBinding>, position: Int, item: KnowledgeTypeInfo?) {
                holder.binding.typeName.text = item?.name?:""
                holder.binding.typeName.onDebouncedClick {
//                    homeVm.itemClick(position,item!!);
                    homeVm.chooseKnowledgeType(position)
                }
                if (item!!.isSelected){
                    holder.binding.typeName.setTextColor(Color.parseColor("#2196F3"))
                    holder.binding.typeName.setSolid(Color.parseColor("#ffffff"))
                }else{
                    holder.binding.typeName.setTextColor(Color.WHITE)
                    holder.binding.typeName.setSolid(Color.parseColor("#33ffffff"))
                }

            }

            override fun onCreateViewHolder(
                context: Context,
                parent: ViewGroup,
                viewType: Int
            ): DataBindingHolder<ItemClassBinding> {
                return DataBindingHolder(R.layout.item_class,parent)
            }
        }
        binding.classRecycler.layoutManager = LinearLayoutManager(requireContext(),RecyclerView.HORIZONTAL,false)
        binding.classRecycler.adapter = konwledgeTypeInfoAdapter
        homeVm.konwledgeTypeInfos.observe(this){
//            konwledgeTypeInfoAdapter.addAll(it)
            konwledgeTypeInfoAdapter.items = it
            konwledgeTypeInfoAdapter.notifyDataSetChanged()
        }

    }

    override fun layoutResId(): Int = R.layout.fragment_home
}